import 'package:efood_multivendor/data/api/api_checker.dart';
import 'package:efood_multivendor/data/model/body/send_points_body.dart';
import 'package:efood_multivendor/data/model/response/transaction_model.dart';
import 'package:efood_multivendor/data/repository/transaction_repo.dart';
import 'package:get/get.dart';

class TransactionController extends GetxController implements GetxService {
  final TransactionRepo walletRepo;
  TransactionController({required this.walletRepo});

  TransactionModel? _transactionModel;
  bool _historyPaginate = false;
  int _historyPageSize = 0;
  List<int> _historyOffsetList = [];
  List<TransactionModel>? _historyTransactionList;
  int _historyOffset = 1;

  bool _isLoading = false;
  bool _isPointSendLoading = false;

  TransactionModel? get transactionModel => _transactionModel;
  List<TransactionModel>? get historyTransactionList => _historyTransactionList;
  bool get historyPaginate => _historyPaginate;
  int get historyPageSize => _historyPageSize;
  int get historyOffset => _historyOffset;
  bool get isLoading => _isLoading;
  bool get isPointSendLoading => _isPointSendLoading;

  Future<void> getTransactionDetails(int offset) async {
    if (offset == 1) {
      _historyOffsetList = [];
      _historyTransactionList = null;
      update();
    }
    _historyOffset = offset;
    if (!_historyOffsetList.contains(offset)) {
      _isLoading = true;
      _historyOffsetList.add(offset);
      Response response = await walletRepo.getTransactionDetails(offset);
      if (response.statusCode == 200) {
        if (offset == 1) {
          _historyTransactionList = [];
        }
        if (PaginatedTransactionModel.fromJson(response.body).data != null) {
          _historyTransactionList?.addAll(
              PaginatedTransactionModel.fromJson(response.body).data ?? []);
        }
        _historyPageSize =
            PaginatedTransactionModel.fromJson(response.body).totalSize ?? 0;
        _historyPaginate = false;
      } else {
        ApiChecker.checkApi(response);
      }
    } else {
      if (_historyPaginate) {
        _historyPaginate = false;
      }
    }
    _isLoading = false;
    update();
  }

  Future<bool> sendPoint(SendPointsBody sendPoints) async {
    _isPointSendLoading = true;
    update();

    Response response = await walletRepo.sendPoints(sendPoints);
    if (response.statusCode == 200) {
      _isPointSendLoading = false;
      update();
      return true;
    } else {
      _isPointSendLoading = false;
      update();
      ApiChecker.checkApi(response);
      return false;
    }
  }

  void showBottomLoader() {
    _historyPaginate = true;
    update();
  }

  void setOffset(int offset) {
    _historyOffset = offset;
  }
}

class TransactionType {
  final String referral_credit = 'Referal-Point-credit';
  final String loyalty_credit = 'Loyality-Point-credit';
  final String wallet_debit = 'Wallet-Balance-Debit-With-Order';
  final String wallet_refund = 'Wallet-Balance-Refund-With-Order-Cancelation';
  final String new_user_wallet_update = "New-User-Wallet-Update";
}
